<html>
<head><meta charset="utf-8"><title>mutable bind and dereference part of a tuple in let · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/mutable.20bind.20and.20dereference.20part.20of.20a.20tuple.20in.20let.html">mutable bind and dereference part of a tuple in let</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="220045640"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/mutable%20bind%20and%20dereference%20part%20of%20a%20tuple%20in%20let/near/220045640" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thomas Bahn <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/mutable.20bind.20and.20dereference.20part.20of.20a.20tuple.20in.20let.html#220045640">(Dec 15 2020 at 21:32)</a>:</h4>
<p>Hi! Is it possible to create a mutable binding to and simultaneously dereference part of a tuple in a let statement?<br>
Like <code>let (mut &amp;last, rest): (usize, &amp;[usize]) = (&amp;0, &amp;[0, 0])</code> but actually working. The error in this case is:</p>
<div class="codehilite"><pre><span></span><code>error: `mut` must be attached to each individual binding
  --&gt; src/example.rs:15:10
   |
15 |     let (mut &amp;last, rest) = (&amp;0, &amp;[0, 0]);
   |          ^^^^^^^^^ help: add `mut` to each binding: `&amp;mut last`
   |
   = note: `mut` may be followed by `variable` and `variable @ pattern`
</code></pre></div>
<p>Either mutably bind the variable <code>let (mut last, rest) = …</code> or dereferencing <code>let (&amp;last, rest) = …</code> is possible, but can I do both in a single let statement?</p>
<p>Of course it's not a problem to split this into two lines (<code>let mut last = last</code>), but I'd like to know whether it's in fact not possible or it's just me not discovering how to do it.</p>
<p>Thanks!</p>



<a name="220045786"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/mutable%20bind%20and%20dereference%20part%20of%20a%20tuple%20in%20let/near/220045786" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/mutable.20bind.20and.20dereference.20part.20of.20a.20tuple.20in.20let.html#220045786">(Dec 15 2020 at 21:33)</a>:</h4>
<p>I think <code>let (&amp;(mut last), rest)</code> will work</p>



<a name="220046072"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/mutable%20bind%20and%20dereference%20part%20of%20a%20tuple%20in%20let/near/220046072" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/mutable.20bind.20and.20dereference.20part.20of.20a.20tuple.20in.20let.html#220046072">(Dec 15 2020 at 21:35)</a>:</h4>
<p><del>I think it has to be <code>let &amp;(mut last, ref rest)</code> instead right?</del> Oh nvm, I misread something</p>



<a name="220046289"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/mutable%20bind%20and%20dereference%20part%20of%20a%20tuple%20in%20let/near/220046289" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thomas Bahn <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/mutable.20bind.20and.20dereference.20part.20of.20a.20tuple.20in.20let.html#220046289">(Dec 15 2020 at 21:37)</a>:</h4>
<p>Oh, this in fact does, thanks! I tried <code>let (mut (&amp;last), rest)</code> and <code>let (mut &amp;last, rest)</code> and <code>let (mut last @ &amp;_, rest)</code> but didn't think of reversing <code>mut</code> and <code>&amp;</code> in the pattern..</p>



<a name="220046573"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/mutable%20bind%20and%20dereference%20part%20of%20a%20tuple%20in%20let/near/220046573" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/mutable.20bind.20and.20dereference.20part.20of.20a.20tuple.20in.20let.html#220046573">(Dec 15 2020 at 21:40)</a>:</h4>
<p>right, the <code>mut</code> has to be directly on the binding name, and it needs parentheses to avoid parsing as <code>&amp;mut</code></p>



<a name="220047038"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/mutable%20bind%20and%20dereference%20part%20of%20a%20tuple%20in%20let/near/220047038" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thomas Bahn <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/mutable.20bind.20and.20dereference.20part.20of.20a.20tuple.20in.20let.html#220047038">(Dec 15 2020 at 21:45)</a>:</h4>
<p>Now with your help I also understand the error message. I wasn't able to find a solution to "<code>mut</code> must be attached to each individual binding" but it's just about using parentheses to group it for the &amp; pattern, preventing it being parsed as <code>&amp;mut</code>.</p>



<a name="220050964"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/mutable%20bind%20and%20dereference%20part%20of%20a%20tuple%20in%20let/near/220050964" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/mutable.20bind.20and.20dereference.20part.20of.20a.20tuple.20in.20let.html#220050964">(Dec 15 2020 at 22:17)</a>:</h4>
<p>it's not helpful that it <em>suggested</em> the invalid <code>&amp;mut last</code> though -- we should get that to add parentheses too</p>



<a name="220051167"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/mutable%20bind%20and%20dereference%20part%20of%20a%20tuple%20in%20let/near/220051167" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/mutable.20bind.20and.20dereference.20part.20of.20a.20tuple.20in.20let.html#220051167">(Dec 15 2020 at 22:19)</a>:</h4>
<p><span class="user-mention" data-user-id="317596">@Thomas Bahn</span> would you care to see if there's an open issue, and/or file one?</p>



<a name="220051761"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/mutable%20bind%20and%20dereference%20part%20of%20a%20tuple%20in%20let/near/220051761" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thomas Bahn <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/mutable.20bind.20and.20dereference.20part.20of.20a.20tuple.20in.20let.html#220051761">(Dec 15 2020 at 22:24)</a>:</h4>
<p>I'll do that! My first contribution to rustc, despite being a small one :)</p>



<a name="220466433"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/mutable%20bind%20and%20dereference%20part%20of%20a%20tuple%20in%20let/near/220466433" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thomas Bahn <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/mutable.20bind.20and.20dereference.20part.20of.20a.20tuple.20in.20let.html#220466433">(Dec 19 2020 at 11:44)</a>:</h4>
<p>The issue is open now: <a href="https://github.com/rust-lang/rust/issues/80186">https://github.com/rust-lang/rust/issues/80186</a></p>



<a name="220492119"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/mutable%20bind%20and%20dereference%20part%20of%20a%20tuple%20in%20let/near/220492119" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thomas Bahn <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/mutable.20bind.20and.20dereference.20part.20of.20a.20tuple.20in.20let.html#220492119">(Dec 19 2020 at 23:06)</a>:</h4>
<p>I found a solution and opened a PR: <a href="https://github.com/rust-lang/rust/pull/80205">https://github.com/rust-lang/rust/pull/80205</a><br>
It was interesting and easier than I thought to locate the responsible code. I guess it was a good first issue :)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>